package com.wan.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wan.entity.SysTUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    private SysUserervice sysUserervice;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //用户权限集合
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        SysTUser sysTUser = new SysTUser();
        sysTUser.setUsername(s);
        QueryWrapper<SysTUser> queryWrapper = new QueryWrapper<>(sysTUser);
        Optional<SysTUser> optionalSysTUser = Optional.ofNullable(sysUserervice.getOne(queryWrapper));
        if (optionalSysTUser.isPresent()) {
            sysTUser = optionalSysTUser.get();
            return new User(sysTUser.getUsername(),sysTUser.getPassword(),grantedAuthorities);
        }else {
            return null;
        }
    }
}
